using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using outlook = Microsoft.Office.Interop.Outlook;

using System.IO;

namespace ESnatch
{
    public partial class Form1 : Form
    {
        MailSystem mailer;
        outlook.MAPIFolder m_selectedFolder;
        string m_savePath = null;
        List<OutputMapping> m_mappings = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            mailer = new MailSystem();
            outlook.Recipient usr = mailer.CurrentUser;
            if (usr != null)
            {
                tbCurrentUserName.Text = usr.Name;
                LoadFolderTree(mailer.Folders);
            }

        }

        private void LoadFolderTree(outlook.Folders folders)
        {
            tvFolders.Nodes.Clear();
            tbSelectedFolder.Text = "";

            foreach(outlook.MAPIFolder folder in folders)
            {
                TreeNode newNode = new TreeNode(folder.Name);
                
                newNode.Tag = new TVTagFolder(folder);
                int n = tvFolders.Nodes.Add(newNode);
                LoadFolderSubTree(newNode);
            }
        }

        private void LoadFolderSubTree(TreeNode parentNode)
        {
            TVTagFolder tgParentFolder = (TVTagFolder)parentNode.Tag;
            if (tgParentFolder == null)
                return;
            if (tgParentFolder.IsLoadedSubTree)
                return;
            outlook.MAPIFolder parentfolder = tgParentFolder.Folder;
            parentNode.Nodes.Clear();

            outlook.Folders folders = parentfolder.Folders;
            foreach (outlook.MAPIFolder folder in folders)
            {
                TreeNode newNode = new TreeNode(folder.Name);
                newNode.Tag = new TVTagFolder(folder);
                TreeNode tempNode = new TreeNode("...");
                newNode.Nodes.Add(tempNode);
                int n = parentNode.Nodes.Add(newNode);               
            }
            tgParentFolder.IsLoadedSubTree = true;
        }

        private void tvFolders_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            TreeNode parentNode = e.Node;
            LoadFolderSubTree(parentNode);
        }

        private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TVTagFolder tf = null;
            if(e.Node != null)
                tf = (TVTagFolder)e.Node.Tag;
            if (tf != null)
            {
                tbSelectedFolder.Text = tf.Folder.Name;
                m_selectedFolder = tf.Folder;
                lblFolderSize.Text = "Items in folder: " + Convert.ToString(tf.Folder.Items.Count);
            }
            else
            {
                m_selectedFolder = null;
                tbSelectedFolder.Text = "";
                lblFolderSize.Text = "No folder currently selected.";
            }
        }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            if (m_selectedFolder == null)
            {
                MessageBox.Show("Need to select a folder to export first.");
                return;
            }
            saveFileDialog1.DefaultExt = "xml";
            saveFileDialog1.FileName = m_selectedFolder.Name + ".xml";
            
            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                tbSaveAs.Text = saveFileDialog1.FileName;
            }
        }

        private void btnMapFields_Click(object sender, EventArgs e)
        {
            SetOutputForm subForm = new SetOutputForm(tbSaveAs.Text, m_selectedFolder, m_mappings);
            DialogResult dr = subForm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                m_mappings = subForm.Mappings;
                checkEnableExportButton();
            }

        }

        private void checkEnableExportButton()
        {
            btnExportNow.Enabled = false;
            if ((m_savePath != null) && !(m_savePath.Equals(String.Empty)))
                if (m_mappings != null)
                    btnExportNow.Enabled = true;
        }

        private void btnExportNow_Click(object sender, EventArgs e)
        {
            //AccessDatabase db = new AccessDatabase();
            //db.createNewDatabase(m_savePath, "EMailDump", m_mappings);
            //db.persistMailFolder(m_mappings, m_selectedFolder);
            XMLDump dump = new XMLDump(m_savePath, OnReportStatus);
            dump.writeMailBox(m_selectedFolder, m_mappings);
            dump.Close();
        }

        private void tbSaveAs_TextChanged(object sender, EventArgs e)
        {
            m_savePath = tbSaveAs.Text;
            checkEnableExportButton();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            lblFolderSize.Text = "";
        }

        protected void OnReportStatus(string status, int nCount)
        {
            lblStatus.Text = status + " " + Convert.ToString(nCount);
            Application.DoEvents();
        }
       

        
     

    }
}